
getCookie=function(name){
	var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr!=null)return unescape(arr[2]); return "";
}
var token= getCookie("token");
$(function(){
	//左侧菜单悬停
	$(".list_menu li").hover(function(){
		var index=$(this).index()
		if(index==0||index==5||index==9){
			$(this).css('backgroundColor','#ffffff')
		}
		else {
			$(this).css('backgroundColor','#FEE631')
		}
	},function(){
		var index=$(this).index()
		if(index==4){
			$(this).css('backgroundColor','#FEE631')
		}
		else{
			$(this).css('backgroundColor','#ffffff')
		}
	})
	//	获取个人资料
	$.ajax({
		url:'http://ww.yc630.com/api/index.php/Buyer/User/userinfo',
		type:"post",
		data:{
			'token':token
		},
		dataType:"JSON",
		success:function(data){
			$('#name').val(data['data']['nickname']);
			$("#gr_name").html(data['data']['nickname'])
			if(data['data']['sex'] == 1){
				$('#nan').attr('checked', 'checked');
			}else if(data['data']['sex'] == 2){
				$('#nv').attr('checked', 'checked');
			}
			$('#phone').html(data['data']['username']);
			$('#email').html(data['data']['email']);
			var avatar = data['data']['avatar'];
			if(avatar==''){
				avatar = '../img/Group%2019.png';
			}
			$('.grzl_wrap .photo img').attr('src',avatar);
			$("#gr_img").attr('src',avatar)
			$("#headPic").attr('src',avatar)
		}
	})
	$.ajax({
		url:'http://ww.yc630.com/api/index.php/Buyer/Order/getaddress',
		type:'post',
		data:{
			"token":token
		},
		dataType:'json',
		success:function(phpdata){
			
			for(i in phpdata['data']){
				var str = '';
				str += '<div class="address edit_address" addressid="'+phpdata['data'][i]['id']+'" username="'+phpdata['data'][i]['username']+'" provice_id="'+phpdata['data'][i]['province']+'" city="'+phpdata['data'][i]['city']+'" area="'+phpdata['data'][i]['area']+'" address="'+phpdata['data'][i]['address']+'" postcode="'+phpdata['data'][i]['postcode']+'" phone="'+phpdata['data'][i]['phone']+'" >';
				if(phpdata['data'][i]['default'] == 1){
					str += '<img src="../img/mrdz.png">';
				}
				
				str += '<table border="0" class="z_tt" cellspacing="0" cellpadding="0" width="100%" height="100%"><tr><td class="z_t_left">收货人:</td><td>'+ phpdata['data'][i]['username'] +'</td></tr><tr><td class="z_t_left">收货地址:</td><td>'+ phpdata['data'][i]['province_name']+ phpdata['data'][i]['city_name']+phpdata['data'][i]['area_name']+phpdata['data'][i]['address']+'</td></tr><tr><td class="z_t_left">邮编:</td><td>'+phpdata['data'][i]['postcode']+'</td></tr><tr><td class="z_t_left">联系方式:</td><td>'+phpdata['data'][i]['phone']+'</td></tr></table></div>';
				$(".address_wrap").append(str);
			}
	        $(".address_wrap").append('<div class="address address_add"><p><span>+</span><br>添加新地址</p></div>');
			
		},
		error:function(res){
			$(".address_wrap").append('<div class="address address_add"><p><span>+</span><br>添加新地址</p></div>');
		}
	})
	

	
	function edit_add_address(str){
		$('.add_address_title').css('display','block');
		$('.add_address_wrap').css('display','block');
		
		if(str=='edit'){
			$('.add_address_title').html('编辑地址');
			$('#address_commit').attr('ad','edit');
		}else if(str=='add'){
			$('.add_address_title').html('添加新地址');
			
			$('#address_commit').attr('ad','add');
		}
		
		$.ajax({
			type:"post",
			url:"http://ww.yc630.com/api/index.php/Buyer/User/getProvince",
			data:{
				"token":token
			},
			dataType:'json',
			success:function(phpdata){
				var opt = '';
				$.each(phpdata['data'], function(k,v) {
					
					opt +='<option value="'+k+'">'+v+'</option>';
				});
				$('select[name="province"]').append(opt);
			}
		});
		$('select[name="province"]').change(function(){
			$('select[name="city"]').html('<option value="0">地区</option>');
			$('select[name="area"]').html('<option value="0">县/市</option>');
			var provinceid = $(this).children(':selected').val();
			
			$.ajax({
				type:"post",
				data:{'provinceid':provinceid,"token":token},
				url:"http://ww.yc630.com/api/index.php/Buyer/User/getCity",
				dataType:'json',
				success:function(phpdata){
					var opt = '';
					$.each(phpdata['data'], function(k,v) {
						
						opt +='<option value="'+k+'">'+v+'</option>';
					});
					$('select[name="city"]').append(opt);
					}
			});
		})
		$('select[name="city"]').change(function(){
			
			$('select[name="area"]').html('<option value="0">县/市</option>');
			var cityid = $(this).children(':selected').val();
			$.ajax({
				type:"post",
				url:"http://ww.yc630.com/api/index.php/Buyer/User/getArea",
				data:{'cityid':cityid,"token":token},
				dataType:'json',
				success:function(phpdata){
					var opt = '';
					$.each(phpdata['data'], function(k,v) {
						
						opt +='<option value="'+k+'">'+v+'</option>';
					});
					$('select[name="area"]').append(opt);
				}

			});
			
		})
	}
	$('.address_wrap').on('click','.address_add',function(){
		$('input[name="username"]').val('');
        $('input[name="address"]').val('');
        $('input[name="mobile"]').val('');
        $('input[name="postcode"]').val('');
        $('select[name="province"]').html('<option value="0">省份</option>');
		$('select[name="city"]').html('<option value="0">地区</option>');
		$('select[name="area"]').html('<option value="0">县/市</option>');
		edit_add_address('add');
	})
	$('.address_wrap').on('click','.edit_address',function(){
		$('.add_address_title').css('display','block');
		$('.add_address_wrap').css('display','block');
		$('#address_commit').attr('ad','edit');
	    $('.add_address_title').html('编辑地址');
        id = $(this).attr('addressid');
        username = $(this).attr('username');
        province = $(this).attr('provice_id');
        city = $(this).attr('city');
        areas = $(this).attr('area');
        address = $(this).attr('address');
        postcode = $(this).attr('postcode');
        phone = $(this).attr('phone');
        
		
        $('input[name="username"]').val(username);
        $('input[name="address"]').val(address);
        $('input[name="mobile"]').val(phone);
        $('input[name="postcode"]').val(postcode);
        $.ajax({
			type:"post",
			url:"http://ww.yc630.com/api/index.php/Buyer/User/getProvince",
			data:{
				"token":token
			},
			dataType:'json',
			success:function(phpdata){
				var opt = '';
				
				$.each(phpdata['data'], function(k,v) {
					
					opt +='<option value="'+k+'"';
					if(k==province){
						opt +='selected="selected">'+v+'</option>'
					}else{
						opt +='>'+v+'</option>';
					}
				});
				$('select[name="province"]').append(opt);
			}
		});
		$.ajax({
				type:"post",
				data:{'provinceid':province,"token":token},
				url:"http://ww.yc630.com/api/index.php/Buyer/User/getCity",
				dataType:'json',
				success:function(phpdata){
					var opt = '';
					$.each(phpdata['data'], function(k,v) {
						
						opt +='<option value="'+k+'"';
						if(k==city){
							opt +='selected="selected">'+v+'</option>'
						}else{
							opt +='>'+v+'</option>';
						}
					});
					$('select[name="city"]').append(opt);
					}
			});
            $.ajax({
				type:"post",
				url:"http://ww.yc630.com/api/index.php/Buyer/User/getArea",
				data:{'cityid':city,"token":token},
				dataType:'json',
				success:function(phpdata){
					var opt = '';
					$.each(phpdata['data'], function(k,v) {
						
						opt +='<option value="'+k+'"';
						if(k==areas){
							opt +='selected="selected">'+v+'</option>'
						}else{
							opt +='>'+v+'</option>';
						}
					});
					$('select[name="area"]').append(opt);
				}

			});
            $('select[name="province"]').change(function(){
				$('select[name="city"]').html('<option value="0">地区</option>');
				$('select[name="area"]').html('<option value="0">县/市</option>');
				var provinceid = $(this).children(':selected').val();
				
				$.ajax({
					type:"post",
					data:{'provinceid':provinceid,"token":token},
					url:"http://ww.yc630.com/api/index.php/Buyer/User/getCity",
					dataType:'json',
					success:function(phpdata){
						var opt = '';
						$.each(phpdata['data'], function(k,v) {
							
							opt +='<option value="'+k+'">'+v+'</option>';
						});
						$('select[name="city"]').append(opt);
						}
				});
		})
		$('select[name="city"]').change(function(){
			
			$('select[name="area"]').html('<option value="0">县/市</option>');
			var cityid = $(this).children(':selected').val();
			$.ajax({
				type:"post",
				url:"http://ww.yc630.com/api/index.php/Buyer/User/getArea",
				data:{'cityid':cityid,"token":token},
				dataType:'json',
				success:function(phpdata){
					var opt = '';
					$.each(phpdata['data'], function(k,v) {
						
						opt +='<option value="'+k+'">'+v+'</option>';
					});
					$('select[name="area"]').append(opt);
				}

			});
			
		})
		
	})
	
    $("#address_commit").click(function(){
    	var ad = $(this).attr('ad');
    	var form = $(this).closest('form');
		var username = form.find('input[name="username"]').val();
		var province = form.find('select[name="province"]').val();		
		var city = form.find('select[name="city"]').val();
		var areas = form.find('select[name="area"]').val();
		var address = form.find('input[name="address"]').val();
		var mobile = form.find('input[name="mobile"]').val();
		var postcode = form.find('input[name="postcode"]').val();;
		if(!username){
			alert('收货人不能为空');
			return false;
		}
		if(province ==0){
			alert('省份不能为空');
			
			return false;
		}
		if(city==0){
			alert('城市不能为空');
			return false;
		}
		if(areas==0){
			alert('区域不能为空');
			return false;
		}
		if(!address){
			alert('详细地址不能为空');
			return false;
		}
		if(!mobile){
			alert('手机不能为空');
			return false;
		}
		if(!postcode){
			alert('邮编不能为空');
			return false;
		}
		if(ad=='edit'){
			var urls = 'http://ww.yc630.com/api/index.php/Buyer/User/changeAddress';
			var datas = {'username':username,'province':province,'city':city,'area':areas,'address':address,'phone':mobile,'postcode':postcode,'id':id,'token':token};
		}else if(ad=='add'){
			var urls = 'http://ww.yc630.com/api/index.php/Buyer/User/addAddress';
			var datas = {'username':username,'province':province,'city':city,'area':areas,'address':address,'phone':mobile,'postcode':postcode,'token':token};
		}
		
		$.ajax({
			type:"post",
			url:urls,
			data:datas,
			dataType:'json',
			success:function(phpdata){
				if(phpdata['code']== 0){
					alert(phpdata['msg']);
					 location.reload() ;
				}else{
					alert(phpdata['msg']);
					return false;
				}
			}
		})
    })
})
